﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace Plateformeur.Engine
{
    public class MovableGameObject : AnimatedGameObject
    {
		protected Vector2 mVelocity;
		protected Vector2 mDirection;
        protected float mSpeed;


        public MovableGameObject(Vector2 pDirection, float pSpeed, string pSpritePath, Vector2 pPositionInitiale, ContentManager pContent, Game pGame) : base(pSpritePath, pPositionInitiale, pContent, pGame)
        {
            mVelocity = Vector2.Zero;
            mDirection = pDirection;

            mSpeed = pSpeed;
        }

        public override void Update(GameTime pTime)
        {

            mPosition = getNextPosition(pTime); //TODO DELTA TIME

            mVelocity += mDirection * (float)pTime.ElapsedGameTime.TotalSeconds;

            if(Math.Abs(mVelocity.X) > mSpeed)
            {
                mVelocity.X = Math.Sign(mVelocity.X) * mSpeed;
            }

            base.Update(pTime);
        }

        public Vector2 getNextPosition(GameTime pTime)
        {
            Vector2 lDir = mVelocity + mDirection;

            float lSpeed = lDir.Length();

            if (lDir.LengthSquared() != 0)
                lDir.Normalize();

            return mPosition + lDir * mSpeed * lSpeed * (float)pTime.ElapsedGameTime.TotalSeconds;
        }

        public Boolean isGrounded()
        {
            if (mVelocity.Y == 0.0f)
                return true;

            return false;
        }

        public void setVelocity(Vector2 pVelocity)
        {
            mVelocity = pVelocity;
        }

        public Vector2 getVelocity()
        {
            return mVelocity;
        }


    }
}
